package com.study.compose.beta

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import com.module.compose.base.BaseActivity
import com.module.compose.base.composable.CommonItem
import com.module.compose.base.composable.CommonPage
import com.module.compose.base.utils.BackHandler
import com.module.compose.base.utils.ToastUtils
import com.module.compose.base.utils.goTo
import com.study.compose.beta.activity.NavActivity
import com.study.compose.beta.activity.SampleActivity
import com.study.compose.beta.activity.coroutines.CoroutinesActivity
import com.study.compose.beta.theme.ScreenBg
import com.zrouter.annotation.Route
import java.util.*

/**
 * 在 2019 年 Google I/O 大会上，Google宣布今后将优先采用 Kotlin 进行 Android 开发，
 * 并且也坚守了这一承诺。Kotlin 是一种富有表现力且简洁的编程语言，不仅可以减少常见代码错误，还可以轻松集成到现有应用中。
 */
@Route("sample/MainActivity")
class MainActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        //VIEW初始化
        setContent {
            Content()
        }
    }

    fun unRe() {

        unregisterReceiver(object : BroadcastReceiver() {
            override fun onReceive(context: Context?, intent: Intent?) {

            }
        })

    }

    override fun onStop() {
        super.onStop()
        try {
            unRe()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}

@Composable
private fun Content() {
    val context = LocalContext.current as BaseActivity
    var time: Long? = null

    //处理返回按钮回调
    BackHandler(backDispatcher = context.onBackPressedDispatcher) {
        if (time == null) {
            time = Date().time
            ToastUtils.show("再次点击退出应用")
        } else {
            if (Date().time - time!! < 2000) {
                context.finish()
            } else {
                ToastUtils.show("再次点击退出应用")
                time = null
            }
        }
    }

    val items =
        listOf(
            "文字",
            "按钮",
            "输入框",
            "Image",
            "Canvas",
            "SwipeToRefresh",
            "状态管理",
            "导航",
            "Grid",
            "Pager",
            "Visibility",
            "布局",
            "列表",
            "协程",
            "参考"
        )
    //通用的页面
    CommonPage(title = "Hello Compose", isShowBack = false) {
        //竖向的列表
        LazyColumn(Modifier.fillMaxSize()) {
            //添加列表的item
            items(count = items.size) { index ->
                CommonItem(
                    content = items[index],
                    iconId = R.mipmap.icon_android,
                    backgroundColor = ScreenBg
                ) {
                    when (items[index]) {
                        "协程" -> context.goTo(CoroutinesActivity::class.java)
                        "导航" -> context.goTo(NavActivity::class.java)
                        else -> {
                            val bundle = Bundle()
                            bundle.putString("title", items[index])
                            context.goTo(SampleActivity::class.java, bundle = bundle)
                        }
                    }

                }
            }
        }
    }
}